#pragma once
#include "EventsSink.h"
#include "WebPageTab.h"
#include <map>
#include <vector>

//------------------------------------------------------
// foreword declaration
//------------------------------------------------------
class CWebPage;
typedef int PAGE_ID;
struct SWebPageInfo
{
	CWebPage				*pPage;
	CBrowserEventSink		*pBrowserSink;
	CHtmlWindowEventSink	*pHtmlWinSink;
};

typedef std::map<PAGE_ID, SWebPageInfo>				WEBPAGES_INFO;	
typedef std::map<PAGE_ID, SWebPageInfo>::iterator	WEBPAGES_INFO_ITER;	
typedef std::vector<PAGE_ID>						WEBPAGES_ORDER;


//------------------------------------------------------
// CWebPageManager
//------------------------------------------------------
class CWebPageManager : public IBrowserEventListener, public IHtmlWndEventListener, public CTabActionWatcher
{
public:
	CWebPageManager(void);
	~CWebPageManager(void);

public:
	CWebPage*	NewWebPage(PAGE_ID id, LPCTSTR pszUrl);
	bool		CloseWebPage(PAGE_ID id);
	bool		Init(HWND hWndMain);					// init com and "atlaxwin80"	
	bool		ActivateWebPage(PAGE_ID id);
	int			FindIdByBrowserSink(IDispatch *pBrowserSink);
	int			FindIdByHtmlWinSink(IDispatch *pHtmlWinSink);
	int			FindIdByWebPage(CWebPage *pPage);
	void		RecalcLayout();

public:
	// from IBrowserEventListener
	virtual void OnTitleChange(IDispatch *pSinkThis, BSTR title);
	virtual void OnPreNavigate(IDispatch *pSinkThis);
	virtual void OnDocumentComplete(IDispatch *pSinkThis, IDispatch *pWebBrowser2, BSTR pszUrl);
	virtual void OnOtherEvent(IDispatch *pSinkThis, DISPID dispIdMember, DISPPARAMS *pDispParams, VARIANT *pVarResult);

	// from IHtmlWndEventListener
	virtual void OnHtmlWndScroll(IDispatch *pSinkThis);

	// from CTabActionWatcher
	virtual void OnTabClick(int id);
	virtual void OnTabDblClick(int id);
	virtual void OnTabClose(int id);
	virtual void OnTabHide(int id);

public:
	static int GetNewId(void){return sm_webpageId++;}

private:
	static PAGE_ID	sm_webpageId;

	WEBPAGES_ORDER	m_order;
	WEBPAGES_INFO	m_webpages;
	HWND			m_hWndMain;
	HWND			m_hWndWebPageParent;
	CTabHost		*m_pTabs;
};
